package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article) {
        articleService.save(article);
        return new Result(true, StatusCode.OK, "保存成功");
    }

    @RequestMapping(value = "/search/{keyword}/{page}/{size}",method = RequestMethod.GET)
    public Result findByTitleLike(@PathVariable String keyword, @PathVariable Integer page, @PathVariable Integer size) {
        Page<Article> titleOrContentPage = articleService.findByTitleOrContent(keyword, page, size);
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult<Article>(titleOrContentPage.getTotalElements(),
                        titleOrContentPage.getContent()));
    }
}
